package com.sakhalin.news.ui.news

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.pagerTabIndicatorOffset
import com.google.accompanist.pager.rememberPagerState
import com.sakhalin.news.CommentsPage
import com.sakhalin.news.network.DataLoadingState
import com.sakhalin.news.ui.other.ErrorView
import com.sakhalin.news.ui.other.LoadingView
import com.sakhalin.news.ui.theme.BlueDark
import com.sakhalin.news.viewmodels.NewsDetailsViewModel
import kotlinx.coroutines.launch

const val NEWS_TEXT_PAGE = 0
const val NEWS_COMMENTS_PAGE = 1

val pages = listOf("Новость", "Комментарии")

@OptIn(ExperimentalPagerApi::class)
@Composable
fun NewsDetailsPages(
    id: String,
    viewModel: NewsDetailsViewModel = viewModel(factory = NewsDetailsViewModel.Factory(id, !MaterialTheme.colors.isLight)),
) {
    val loadingState by viewModel.loadingState.collectAsState()
    val newsDetails by viewModel.newsDetails.collectAsState()
    val pagerState = rememberPagerState()
    val scope = rememberCoroutineScope()
    val isLightTheme = MaterialTheme.colors.isLight

    Column(
        modifier = Modifier.background(if (isLightTheme) Color.White else Color.Black)
    ) {

        when(loadingState){
            is DataLoadingState.Loading -> {
                LoadingView()
            }
            is DataLoadingState.LoadingSuccess -> {
                TabRow(
                    backgroundColor = if (isLightTheme) Color.White else Color.Black,
                    contentColor = if (isLightTheme) BlueDark else Color.White,
                    selectedTabIndex = pagerState.currentPage,
                    indicator = { tabPositions ->
                        TabRowDefaults.Indicator(
                            modifier = Modifier.pagerTabIndicatorOffset(pagerState, tabPositions)
                        )
                    }
                ) {
                    pages.forEachIndexed { index, title ->
                        Tab(
                            text = { Text(text = title) },
                            selected = pagerState.currentPage == index,
                            modifier = Modifier.fillMaxWidth(),
                            onClick = { scope.launch { pagerState.animateScrollToPage(index) } },
                        )
                    }
                }

                //Создаем ViewPage-ры c списком новостей
                HorizontalPager(
                    state = pagerState,
                    count = pages.size,
                    modifier = Modifier.fillMaxSize(),
                    verticalAlignment = Alignment.Top
                ) { page ->
                    when (page) {
                        NEWS_TEXT_PAGE -> {
                            NewsDetails(newsHtml = newsDetails.text)
                        }
                        NEWS_COMMENTS_PAGE -> CommentsPage()
                    }
                }
            }
            is DataLoadingState.ErrorLoading -> {
                ErrorView(errorText = (loadingState as DataLoadingState.ErrorLoading).error, onClickRetry = {
                    viewModel.getNews(id)
                })
            }
        }

    }
}

